package com.cc.demo2;

import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

//查询索引中的所有数据
public class UpdateClient1 {
    public static  void main(String[] args) throws IOException {
        //创建java和es的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        System.out.println(client.indices());

        //修改文档
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("good").id("1");
        //指定修改的信息
        updateRequest.doc(XContentType.JSON,"name","华为手机");
        //发送请求
        UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);

        //输出响应结果
        System.out.println("_index:"+ response.getIndex());
        System.out.println("_id:"+ response.getId());
        System.out.println("_type:"+ response.getType());
        DocWriteResponse.Result result = response.getResult();
        System.out.println("响应状态:"+result);



        //关闭连接
        client.close();
    }
}
